﻿using Infrastructure.Web;
using MyApplication;
using MyData.Dto;
using MyData.Models;
using MyWeb.App_Start;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyWeb.Controllers
{
    [HandlerLogin]
    public class DepartController : BaseController
    {

        // GET: Depart

        [HandlerLogin]
        public ActionResult Index()
        {
            return View();
        }
        [HandlerLogin]
        public ActionResult DepartView(string id)
        {
            if (!String.IsNullOrEmpty(id))
            {
                var entity = departApp.GetDepart(Guid.Parse(id));
                return View(entity);
            }
            return View(new DepartDto());

        }
        public ActionResult ConfigMobile(string Id)
        {
            ConfigMobileDto configMobileDto = departMobileRLApp.GetConfigMobileDto(Guid.Parse(Id));
            return View(configMobileDto);
        }
        public ActionResult GetDeparts(QueryListModel query, string Name,string OrderName)
        {
            var result = departApp.GetDeparts(query, Name,OrderName);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult SaveDepart(string Id, string Name, string Remark,int IsShow,int OrderNum)
        {
            return base.Try(() =>
            {
                Depart depart = new Depart();
                if (!String.IsNullOrEmpty(Id))
                    depart.Id = Guid.Parse(Id);
                depart.Name = Name;
                depart.Remark = Remark;
                depart.IsShow = IsShow;
                depart.OrderNum = OrderNum;
                departApp.SaveDepart(depart);
            });
        }
        [HttpPost]
        public JsonResult DepartDelete(string Id)
        {
            return base.Try(() =>
            {
                var departId = Guid.Parse(Id);
                var exist = userInfoApp.IsExistUser(departId);
                if (exist)
                {
                    throw new KnownException("该部门下有用户,请先删除用户");
                }

                departApp.DepartDelete(departId);
            });

        }
        [HttpPost]
        public JsonResult SaveConfigMobile(Guid Id, List<Guid> Mobiles)
        {
            return base.Try(() =>
            {
                departMobileRLApp.SaveConfigMobile(Id, Mobiles);
            });
        }
        public ActionResult GetDepartIn(Guid Id)
        {
           ViewBag.Names = departApp.GetDepartIn(Id);
            return View();
        }
    }
}